package com.zoke.password.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.andrognito.patternlockview.PatternLockView;
import com.andrognito.patternlockview.listener.PatternLockViewListener;
import com.zoke.password.Conf;
import com.zoke.password.R;
import com.zoke.password.base.BaseActivity;
import com.zoke.password.model.User;
import com.zoke.password.tools.PersistTool;
import com.zoke.password.tools.UToast;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

@ContentView(R.layout.activity_pattern_lock)
public class PatternLockActivity extends BaseActivity implements PatternLockViewListener {


    @ViewInject(R.id.patter_lock_view)
    PatternLockView patternLockView;
    @ViewInject(R.id.profile_name)
    TextView profile_name;

    int type = 0;

    public static final int LOCK = 1001;


    private int setCount = 1;

    private String firstPwd;
    private String secondPwd;

    private boolean is;

    /**
     * 0 是输入密码
     * 1是设置密码
     *
     * @param activity
     * @param type
     */
    public static void open(Activity activity, int type) {
        open(activity, type, false);
    }


    /**
     * 0 是输入密码
     * 1是设置密码
     *
     * @param activity
     * @param type
     */
    public static void open(Activity activity, int type, boolean isFirst) {
        Intent intent = new Intent(activity, PatternLockActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("is", isFirst);
        activity.startActivityForResult(intent, LOCK);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        patternLockView.addPatternLockListener(this);
        patternLockView.setInputEnabled(true);
        type = getIntent().getIntExtra("type", 0);
        is = getIntent().getBooleanExtra("is", false);
        profile_name.setText(getResources().getString(type == 0 ? R.string.password : R.string.password_set));
        if (User.getUser() == null) return;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        if (mImmersionBar != null)
            mImmersionBar.statusBarDarkFont(true)
                    .statusBarView(R.id.status)
                    .init();
    }

    @Override
    public void onStarted() {

    }

    @Override
    public void onProgress(List<PatternLockView.Dot> progressPattern) {

    }

    @Override
    public void onBackPressed() {
        if (!is) {
            finish();
            setResult(LOCK, null);
        }
    }

    @Override
    public void onComplete(List<PatternLockView.Dot> pattern) {
        String password;
        if (type == 0) {
            password = getPassword(pattern);
            //密码相同
            if (password.equals(User.getUser().password)) {
                Intent intent = new Intent();
                intent.putExtra("result", "1");
                setResult(LOCK, intent);
                finish();
                return;
            }
            //密码不同
            UToast.show(PatternLockActivity.this, "密码输入错误");
            patternLockView.clearPattern();
            return;
        }
        //处理设置密码
        if (setCount == 1) {
            //第一次设置
            firstPwd = getPassword(pattern);
            setCount = 2;
            profile_name.setText(getResources().getString(R.string.password_deal));
            patternLockView.clearPattern();
        } else if (setCount == 2) {
            secondPwd = getPassword(pattern);
            patternLockView.clearPattern();
            if (firstPwd.equals(secondPwd)) {
                //两次密码一致
                User.getUser().password = firstPwd;
                User.getUser().save(User.getUser());
                UToast.show(PatternLockActivity.this, "密码设置成功");
                if (is) {
                    //第一次设置密码 直接跳转到首页
                    PersistTool.saveBoolean(Conf.KEY_PASSWORD, true);
                    open(PatternLockActivity.this, MainActivity.class);
                    finish();
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("result", "1");
                    setResult(LOCK, intent);
                    finish();
                }
            } else {
                //两次密码不一yang
                firstPwd = "";
                secondPwd = "";
                profile_name.setText(getResources().getString(R.string.password_set));
                UToast.show(PatternLockActivity.this, "两次密码不一致，请重新设置");
                setCount = 1;
            }

        }
    }

    private String getPassword(List<PatternLockView.Dot> pattern) {
        String password = "";
        for (PatternLockView.Dot dot : pattern) {
            password = password + String.valueOf(dot.getRow()) + dot.getColumn();
        }
        LogUtil.e(password);
        return password;
    }

    @Override
    public void onCleared() {

    }

}
